package com.tms.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.entity.Book;
import com.tms.entity.BookCategory;
import com.tms.entity.PageBean;
import com.tms.entity.Result;
import com.tms.service.BookCategoryService;
import com.tms.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 教材的详细信息(Book)表控制层
 *
 * @author makejava
 * @since 2024-12-27 01:55:21
 */
@RestController
@RequestMapping("book")
public class BookController {
    @Autowired
    private BookService bookService;


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Book book) {
        return bookService.save(book) ? Result.success() : Result.error();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Book book) {
        return bookService.updateById(book) ? Result.success() : Result.error();
    }

    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id) {
        return bookService.removeById(id) ? Result.success() : Result.error();
    }

    @GetMapping("/pageList")
    public Result pageList(@RequestBody PageBean pageBean) {
        HashMap params = pageBean.getParams();

        String bookName = params.get("bookName").toString();

        Page<Book> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());

        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(bookName) && !"null".equals(bookName)) {
            lambdaQueryWrapper.like(Book::getBookName, bookName);
        }

        IPage result = bookService.pageList(page, lambdaQueryWrapper);

        return Result.success(result.getRecords());
    }
}

